<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ZHOUYANG
 * Date: 13-3-30
 * Time: 下午2:37
 */
 
class UserController extends Controller{
    
    public function index(){
        //取得博文列表
        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $user_name = $this->input()->post('user_name');

        $user_list = Model::getInstance('user')->getUserList($curr_page,$per_page,$user_name);

        $data = array(
            'user_list'=>$user_list,
        );

        $template = array('data'=>$data);
        Template::show($template);
    }
    
    public function login(){
        $user_name = $this->input()->post('user_name');
        $password = substr(md5($this->input()->post('password').PASSWORD_PREFIX),0,16);

        $user_info = Model::getInstance('user')->loginUser($user_name,$password);

        if($user_info){
            $this->session()->set('user_id',$user_info['user_id']);
            setcookie(SITE_PREFIX.'user_id', $user_info['user_id'], time()+3600*24, '/', COOKIE_DOMAIN);
            Model::getInstance('user')->login($user_info['user_id']);
            redirect('index.php');
        }else{
            $data = array(
                'message'=>'登录失败，用户名或密码错误！',
                'url'    =>site_url('login.php'),
            );
            $template = array('view'=>'error.php','data'=>$data);
            Template::show($template);
        }
    }

    public function add(){
        Template::show();
    }

    public function del(){

        $user_id = $this->input()->get('user_id');
        $ret = Model::getInstance('user')->delUser($user_id);

        if($ret){
            Model::getInstance('catehasuser')->delByUserId($user_id);
            Model::getInstance('userhasrole')->delByUserId($user_id);
            $data = array(
                "statusCode"=>"200",
                "navTabId"=>"user",
            );
            echo Response::JSON($data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON($data);
        }
    }

    public function edit(){
        $user_id = (int)$this->input()->get('user_id');

        $editrole = (int)$this->input()->get('editrole',1);
        
        $user_info = Model::getInstance('user')->getUserById($user_id);

        $roles = Model::getInstance('user')->getUserRoles($user_id);

        $_role_ids = $_role_names = array();

        foreach($roles as $v){
            $_role_ids[] = $v['role_id'];
            $_role_names[] = $v['role_name'];
        }

        $is_super_admin = Model::getInstance('user')->isSuperAdmin($this->session()->get('user_id'));

        $data = array(
            'role_ids'=>implode(',',$_role_ids),
            'role_names'=>implode(',',$_role_names),
            'user_info'=>$user_info,
            'editrole'=>$editrole,
            'is_super_admin'=>$is_super_admin
        );

        $template = array('data'=>$data);
        Template::show($template);
    }

    public function role(){
        $user_id = (int)$this->input()->get('user_id');

        $cates = Model::getInstance('user')->getUserCates($user_id);

        $cate_ids = array();
        foreach($cates as $v){
            $cate_ids[] = $v['cate_id'];
        }

        $menu_arr = Model::getInstance('cate')->getTree();

        $data = array(
            'user_id'=>$user_id,
            'menu_arr'=>$menu_arr,
            'cate_ids'=>$cate_ids,
        );

        $template = array('data'=>$data);
        Template::show($template);
    }

    public function setRole(){
        $user_id = (int)$this->input()->get('user_id');
        
        $roles = Model::getInstance('role')->getRoles();
        $user_roles = Model::getInstance('user')->getUserRoles($user_id);
        
        $role_ids = array();
        foreach($user_roles as $v){
            $role_ids[] = $v['role_id'];
        }

        $data = array(
            'user_id'=>$user_id,
            'roles'=>$roles,
            'role_ids'=>$role_ids,
        );

        $template = array('data'=>$data);
        Template::show($template);
    }

    public function lookUser(){
        $type = $this->input()->get('type','');
        if($type == 2){
            $users = Model::getInstance('user')->getRoleUsers(2);
        }else{
            $users = Model::getInstance('user')->getUsers();
        }
        $parent_rel = $this->input()->get('rel');
        $data = array(
            'users'=>$users,
            'parent_rel'=>$parent_rel
        );

        if($type == 3){
            $fun_id = (int)$this->input()->get('fun_id');
            $users = Model::getInstance('fun')->getUsers($fun_id);

            $arr1 = array();
            foreach($users as $v){
                $arr1[] = $v['user_id'];
            }

            $arr2 = array();
            foreach($data['users'] as $v){
                if(!in_array($v['user_id'],$arr1)){
                    $arr2[] = $v;
                }
            }
            $data['users'] = $arr2;
            $data['fun_id'] = $fun_id;
        }

        $template = array('data'=>$data);
        Template::show($template);
    }
}